/**
 *@description:TODO
 * @author: cai
 *@time:2017年10月13日上午11:56:08
 */
package com.pos.modules.inquery;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.pos.BaseActivity;
import com.pos.R;
import com.pos.modules.inquery.adapter.BillListAdapter;
import com.pos.modules.inquery.adapter.ChooseAdapter;
import com.pos.modules.inquery.bean.BillBean;
import com.pos.net.base.Request.Method;
import com.pos.net.netparam.NetParams;
import com.pos.net.netparam.RequestUrls;
import com.pos.net.request.interfa.BaseRequestListener;
import com.pos.net.request.interfa.JsonRequestListener;
import com.pos.toolbox.FinancialUtils;
import com.pos.toolbox.Tools;
import com.pos.widgets.ActionBarManager;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.content.res.Configuration;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

/**
 * @description:交易查询主页
 * @author cai
 * @time:2017年10月13日上午11:56:08
 */
public class InqueryActivity extends BaseActivity implements OnClickListener {
	private BillListAdapter adapter;
	private List<BillBean> billList = new ArrayList<>();
	private ListView listView;
	private TextView text_date, text_search, text_orderCount, text_orderTotalAmount, text_orderActToalAmount,
			text_discountAmount;
	private Spinner choose_spinner;
	private String orderDate;
	private Calendar c = Calendar.getInstance();
	private Gson gson = new Gson();
	private String orderStatus = "";

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	public void initView() {
		setContentView(R.layout.inquery_layout);
		listView = bindId(R.id.listView);
		text_date = bindId(R.id.text_date);
		text_search = bindId(R.id.text_search);
		text_orderCount = bindId(R.id.text_orderCount);
		text_orderTotalAmount = bindId(R.id.text_orderTotalAmount);
		text_orderActToalAmount = bindId(R.id.text_orderActToalAmount);
		text_discountAmount = bindId(R.id.text_discountAmount);
		choose_spinner = bindId(R.id.spinner);
	}

	@Override
	public void initData() {
		ActionBarManager.init(this, "交易查询", true, null, null);
		orderDate = Tools.stampToDate2(System.currentTimeMillis() + "");
		text_date.setText(orderDate);
		ChooseAdapter chooseAdapter = new ChooseAdapter(this);
		List<String> list = new ArrayList<>();
		list.add("全部");
		list.add("成功");
		list.add("退货");
		chooseAdapter.setDatas(list);
		choose_spinner.setAdapter(chooseAdapter);
	}

	@Override
	public void initListener() {
		text_date.setOnClickListener(this);
		text_search.setOnClickListener(this);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				BillBean item = (BillBean) parent.getAdapter().getItem(position);
				Intent intent = new Intent(mContext, BillDetailActivity.class);
				intent.putExtra("orderCode", item.getOrderCode());
				startActivity(intent);
			}
		});
		choose_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
				if (i == 0) {
					orderStatus = "1";
				} else if(i==1){
					orderStatus = "0";
				}else {
					orderStatus = "4";
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> adapterView) {

			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.text_date:
			new DatePickerDialog(this, new OnDateSetListener() {

				@Override
				public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
					String month = "";
					String day = "";
					if (monthOfYear + 1 < 10) {
						month = "0" + (monthOfYear + 1);
					} else {
						month = "" + (monthOfYear + 1);
					}
					if (dayOfMonth < 10) {
						day = "0" + dayOfMonth;
					} else {
						day = "" + dayOfMonth;
					}
					orderDate = year + "-" + month + "-" + day;
					text_date.setText(orderDate);
				}
			}, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH)).show();
			break;
		case R.id.text_search:
			if (TextUtils.isEmpty(orderDate)) {
				showShortToast("请请选择日期");
			} else {
//				netRequest.startRequest(RequestUrls.hostUrl + RequestUrls.posQueryOrders, Method.POST,
//						NetParams.posQueryOrders(mContext, orderDate), 0, listener);
				netRequest.startRequest(RequestUrls.hostUrl + RequestUrls.posQueryOrders, Method.POST,
						NetParams.posQueryOrders(mContext, orderDate,orderStatus), 0, listener);
			}
			break;

		default:
			break;
		}

	}

	private BaseRequestListener listener = new JsonRequestListener() {

		@Override
		public void onStart(int requestId) {
			showDialg();
		}

		@Override
		public void onError(int requestId, int errorCode, String errorMsg) {
			disMissDialog();
			dealError(errorCode, errorMsg);
		}

		@Override
		public void onSuccess(int requestId, String url, JSONObject jsonResult) {
			disMissDialog();
			if (requestId == 0) {
				int retCode = jsonResult.optInt("retCode", 0);
				if (retCode == 1) {
					showShortToast("查询失败");
					return;
				}
				JSONArray ordersList = jsonResult.optJSONArray("ordersList");
				if (ordersList == null || ordersList.length() == 0) {
//					showShortToast("暂无记录");
					listView.setVisibility(View.GONE);
					text_orderCount.setText("0");
					text_orderTotalAmount.setText("0");
					text_orderActToalAmount.setText("0");
					text_discountAmount.setText("0");
					return;
				}
				listView.setVisibility(View.VISIBLE);
				billList = gson.fromJson(ordersList.toString(), new TypeToken<List<BillBean>>() {
				}.getType());
				adapter = new BillListAdapter(mContext, billList);
				listView.setAdapter(adapter);
				text_orderCount.setText(jsonResult.optString("orderCount"));
				text_orderTotalAmount.setText(FinancialUtils.fenToYuan(jsonResult.optLong("orderTotalAmount", 0)));
				text_orderActToalAmount.setText(FinancialUtils.fenToYuan(jsonResult.optLong("orderActToalAmount", 0)));
				text_discountAmount.setText(FinancialUtils.fenToYuan(jsonResult.optLong("discountAmount", 0)));
			}

		}
	};

}
